<?php	
	defined('_JEXEC') or die('Restricted access');
    //
    require_once(CUTEENGINE_NAMESPACES_PATH."strings_utility_namespace.php");
    require_once(CUTEENGINE_BASE_COMPONENTS_PATH."log_class.php");
    //
    // t_connection
    //
    class t_connection
    {
		// property declaration
        // public
        // C
        public $connected = false;
        // D
		public $db_selected = false;        
        //L
        public $log = false; //t_log
		//M
		public $mysqli = false;
		
        //
        public function __construct() 
		{
           // global $g_ambient_namespace;
            //$this->log = $g_ambient_namespace->log;
			$this->log = new t_log;
            //echo "$a_sql_str";
           // $this->sql_str = $a_sql_str;
		}
        // C
        public function close_connection ($a_ignore_mysql_close = false)
		{
			$this->log->add_log_on_page("close_connection",true);
            try
            {
                if ($a_ignore_mysql_close == false)
					$this->mysqli->close();
                
            }    
            catch(Exception $e)
            {
				
                $this->log->add_log_error($e,NULL,true);    
                
            }
			$this->connected = false;			
		}
		// O
		public function open_connection ()
		{
			global $g_database_namespace;
			global $g_ambient_namespace;
			
            $this->log->add_log_on_page("open_connection",true);
			
            //
            $this->connected = false;
            //
            $this->mysqli = new mysqli($g_database_namespace->server_address,
                $g_database_namespace->user_name,$g_database_namespace->user_password);
            if (mysqli_connect_error() == null)
            {
                if ($this->db_selected = $this->mysqli->select_db($g_database_namespace->database_name))
                {
                    $this->connected = true; 
					$this->set_time_zone();
					$this->set_character_set();

				}
                else
                {
					$this->log->add_log_error("Errore selezionando il database '".$g_database_namespace->database_name."' a causa di: ".$this->mysqli->error,NULL,true);
                }
            }
            else
            {
				$this->log->add_log_error("connessione al server database '".$g_database_namespace->server_address."' non riuscita a causa di: ".$this->mysqli->error,NULL,true);
            }
			//echo '//oLink aperto';
			return $this->mysqli;
			
		}    
		
		public function set_time_zone()
		{
			global $g_ambient_namespace;
			//echo "g_ambient_namespace->mysql_time_zone: $g_ambient_namespace->mysql_time_zone";
			if (($this->connected == true) && ($g_ambient_namespace->mysql_time_zone != ''))
				$this->mysqli->multi_query("SET time_zone = '".$g_ambient_namespace->mysql_time_zone."'");
		}

		public function set_character_set()
		{
			global $g_ambient_namespace;
			//echo "g_ambient_namespace->mysql_character_set: $g_ambient_namespace->mysql_character_set";
			if (($this->connected == true) && ($g_ambient_namespace->mysql_character_set != ''))
				$this->mysqli->multi_query("SET names ".$g_ambient_namespace->mysql_character_set);
		}		
    }
    //
	//t_query_parameter
	//
	class t_query_parameter
	{
	    const NOT_NULL_FLAG = 1;                                                                              
	    const PRI_KEY_FLAG = 2;                                                                               
	    const UNIQUE_KEY_FLAG = 4;
	    const BLOB_FLAG = 16;
	    const UNSIGNED_FLAG = 32;
	    const ZEROFILL_FLAG = 64;
	    const BINARY_FLAG = 128;
	    const ENUM_FLAG = 256;
	    const AUTO_INCREMENT_FLAG = 512;
	    const TIMESTAMP_FLAG = 1024;
	    const SET_FLAG = 2048;
	    const NUM_FLAG = 32768;
	    const PART_KEY_FLAG = 16384;
 	    const GROUP_FLAG = 32768;
  	    const UNIQUE_FLAG = 65536;
		
		// property declaration
        // private
        // B
		private $bof_booked = false;

        // E
        private $eof_booked = false;
        // F
        private $fields_count = -1;
		// I
		private $in_transaction = false;
        
		//L
		private $last_error = '';
		//M
		public $mysqli = false;
				
        // T
        private $table_information = false;        
		// property declaration
        // public
		// db_position_of_value. 
		// The level is calculated counting the occurence of the character self::separator_level_character
		// The position of the layer is represented by the number extract between characters
		const db_position_of_value_level_0 = 0;  //position level 1  es: @a@b@c@
		const db_position_of_value_level_1 = 1;  // position level 1_position level 2 es: @|a|a1|an|@|b|b1|b2|@|c|c1|c2|@
        //C
        public $connection; //t_connection
        public $current_row_no = -1;
        // F
        public $function_result = '';
		public $fields_list = array();
		// L
        public $log = false; //t_log
		// M
		public $main_table_name = '';
		// P
		public $parameter_index_for_argument_value = 0;
		public $parameters_names = array();
		public $parent_component_class = '';
		// R
		public $result_type = MYSQLI_BOTH;
        // V
        public $value_parameter = array();
		
        
		// S
        const separator_level_0 = '@';
        const separator_level_1 = '|';
		const separator_level_character = "_";
        //
        public $sql_result_query = null;
		public $sql_str = false;
        public $sql_values = false;
		//
		const char_parameter = ":";
        // method declaration
		// private
		// D
		private function db_result($a_result,$a_row,$a_field)
		{ 
		  $a_result->data_seek($a_row);
		  $local_arr = $a_result->fetch_assoc(); 
		  $local_return = $local_arr[$a_field]; 
		  return $local_return; 
		}
		// G
		private function get_char_parameter_position($a_sql,$a_offset = 0)
		{
			$local_return = stripos($a_sql,self::char_parameter,$a_offset); 
			if (($local_return > 0) && ((substr($a_sql,$local_return-1,1) == "\\") || (substr($a_sql,$local_return+1,1) == "/")))
				$local_return = $this->get_char_parameter_position($a_sql,$local_return+1);
			return $local_return;
		}
        // method declaration
		// public
		public function __construct($a_sql_str,$a_connection = null,$a_parent_component_class = '') 
		{
			global $g_ambient_namespace;
            $this->log = $g_ambient_namespace->log;
            //echo "$a_sql_str";
            if (isset($a_connection))
            {
               // echo "<br> a_connection OK <br>";
                $this->connection = $a_connection;
				$this->mysqli = $a_connection->mysqli;
            }    
            else    
            {
               // echo "<br> a_connection NO <br>";
                $this->connection = new t_connection;
            }    
                
            $this->sql_str = $a_sql_str;
			//print_r($matches);
			if (preg_match("/select.*from\s+(`?\w*`?\.?`?\w*`?\.?`?\w*`?)/im",$a_sql_str,$matches) >0) 
			{
				$this->main_table_name  = self::prepare_table_name($matches[1]);
				
			}
			else if (preg_match("/insert\s+INTO\s+(`?\w*`?\.?`?\w*`?\.?`?\w*`?)/im",$a_sql_str,$matches) >0) 
			{
				$this->main_table_name  = self::prepare_table_name($matches[1]);
				
			}
			else if (preg_match("/update\s+(`?\w*`?\.?`?\w*`?\.?`?\w*`?)/im",$a_sql_str,$matches) >0) 
			{
				$this->main_table_name  = self::prepare_table_name($matches[1]);
				
			}
			else
			{
				$this->main_table_name  = '';
				//throw new Exception('query table name not found');	
			}
            //echo "this->main_table_name:". $this->main_table_name."<br>";
			$this->parent_component_class = $a_parent_component_class;
			//echo "this->parent_component_class:". $this->parent_component_class."<br>";
            
		}
		// A
		public function add_parameter_as_last ($a_value, $skip_real_escape_string = false)
		{   
            $local_index = (count($this->value_parameter));
            //echo "local_index: $local_index - a_value: $a_value";
			if ($skip_real_escape_string)
				$this->value_parameter[$local_index] = $a_value; 
			else
				$this->value_parameter[$local_index] = $this->mysqli->real_escape_string($a_value); 
		}
		// B
        public function bof()
		{   
            return (($this->bof_booked) && ($this->is_first_record()));
		}         
		// C        
		public function close_all_results ()
		{
			while( $this->mysqli->next_result());
		}	
		public function close_connection ($a_ignore_mysql_close = false)
		{
            $this->connection->close_connection($a_ignore_mysql_close);
		}
        // E
        public function eof()
		{   
            return (($this->eof_booked) && ($this->is_last_record()));
		} 
        
        public function execute_function_from_current_record ($a_function_obj,$a_function_name,$a_function_argouments = null)
		{   
            
            //echo __METHOD__ . " - a_function_name: ".$a_function_name . "<BR>";
          //  if (!$this->connection->connected)
                //$this->open_connection();
            //    
            try
            {
				if ($this->connection->connected ) 
				{                    
                    //echo "local_rows_count: $local_rows_count <br>";
                    $a_function_obj->$a_function_name($this,$this->current_row_no,$local_rows_count,
                        $a_function_argouments);
                    
                }
				else
				{
					$this->log->add_log_error("Database not connected",NULL,true);					
					$this->last_error = "Database not connected";
				}
            }    
            catch(Exception $e)
            {
                $this->last_error = $e->getMessage();
				$this->log->add_log_error("errore imprevisto eseguendo la funzzone execute_function_from_current_record: ".$e->getMessage(),NULL,true);//$this->mysqli->error());
				
            }
           // if (!$this->connection->connected)
             //   $this->connection->close_connection();
		}        
		public function execute_function_from_fields_result ($a_function_obj,$a_function_name,$a_function_argouments = null)
		{  		
			$local_result = '';
            if ($this->connection->connected ) 
			{
				if (!$this->is_sql_result_query_failed())
                {
                    if ($this->has_sql_result_query_got_result_set())
					{
						$local_rows_count = mysqli_num_rows($this->sql_result_query);
						//echo "local_rows_count:$local_rows_count<br>";
						$i = 0;
						if ($this->fields_count == -1)
							$this->fields_count = mysqli_num_fields($this->sql_result_query);
						while ($i < $this->fields_count) 
						{
							//echo "Information for column $i:<br />\n";
							$local_field = mysqli_fetch_field_direct($this->sql_result_query, $i);
							if (!$local_field) {
								//echo "No information available<br />\n";
							}
							$local_field->primary_key = ($local_field->flags & self::PRI_KEY_FLAG) == self::PRI_KEY_FLAG;
							$local_field->unique_key = ($local_field->flags & self::UNIQUE_KEY_FLAG) == self::UNIQUE_KEY_FLAG;
							$local_field->auto_increment = ($local_field->flags & self::AUTO_INCREMENT_FLAG) == self::AUTO_INCREMENT_FLAG;
							$local_field->not_null = ($local_field->flags & self::NOT_NULL_FLAG) == self::NOT_NULL_FLAG;
							$local_field->numeric = ($local_field->flags & self::NUM_FLAG) == self::NUM_FLAG;
							
							//echo "local_field->primary_key: $local_field->primary_key<br>";
							$local_result .= $a_function_obj->$a_function_name($this,$local_field,$i,$this->fields_count,
								$this->current_row_no,$local_rows_count,$a_function_argouments);
							/*echo "<pre>
						Name:     $local_field->name<br>
						Table:    $local_field->table<br>
						max. Len: $local_field->max_length<br>
						Flags:    $local_field->flags<br>
						Type:     $local_field->type<br>
						</pre>";*/
							$i++;
						} 			
					}	
                }
                else
                {
                    $this->log->add_log_error('$this->sql_result_query not valid',NULL,true);					
                    $this->last_error = '$this->sql_result_query not valid';
                }
            }
			else
			{
				$this->log->add_log_error("Database not connected",NULL,true);					
				$this->last_error = "Database not connected";
			}	
            return $local_result;
        }
		public function has_sql_result_query_got_result_set()
		{
			return (($this->sql_result_query !== null) && ($this->sql_result_query !== false) &&
				($this->mysqli->error == '') && (!$this->is_query_without_result()));
		}		
		public function is_sql_result_query_failed()
		{
			return ((($this->is_query_without_result()) && ($this->sql_result_query === false)) || ($this->sql_result_query === null) || ($this->mysqli->error != ''));
		}
		
		public function execute_function_from_query_result ($a_function_obj,$a_function_name,$a_function_argouments = null)
		{   
            
            //echo "a_function_name: ".$a_function_name;
          //  if (!$this->connection->connected)
                //$this->open_connection();
            //    
            $this->function_result = '';
            try
            {
            
				$local_sql = $this->retrive_sql_query_with_value_parameter($this->sql_str);
               // echo "local_sql: $local_sql<br>";
				$this->log->add_log_on_page("execute_function_from_query_result - local_sql: $local_sql",true);
				//echo "this->connection->connected:". $this->connection->connected;
				if ($this->connection->connected ) 
				{
					//echo "this->sql_result_query: " . $this->sql_result_query ."<br>";
					
					if ($this->is_sql_result_query_failed())
                    {
                        //echo "execute_query ...";
						//$this->mysqli->multi_query($local_sql);
						$this->sql_result_query = $this->mysqli->multi_query($local_sql);
						if (!$this->is_query_without_result())
						{
							//echo "store<br>";
							$this->sql_result_query = $this->mysqli->store_result();
						}
						if (!$this->is_sql_result_query_failed())
						{
							$this->current_row_no = -1;
							$this->bof_booked = false;
							$this->eof_booked = false;
							$this->next_record();
							//echo "local_rows_count: ". $local_rows_count ."<br>";
						}	
                    }
                    else
                    {
						//echo "first ...";
                        $this->first();
                    }
                    if (!$this->is_sql_result_query_failed())
					{   
						if ($this->has_sql_result_query_got_result_set())
						{
							
							$local_skip_fetch = false;
							$local_rows_count = mysqli_num_rows($this->sql_result_query);
							$this->log->add_log_on_page("local_rows_count : $local_rows_count",true);
							
							//echo "local_rows_count: ". $local_rows_count ."<br>";
							//echo "local_skip_fetch: ".$local_skip_fetch ."<br>";
							while (!$this->eof()) 
							{ 
								//
								$this->log->add_log_on_page("this->current_row_no : $this->current_row_no",true);
								$local_skip_fetch =  ($a_function_obj->$a_function_name($this,$this->current_row_no,$local_rows_count,
									$a_function_argouments) );
								//echo "local_skip_fetch: $local_skip_fetch<br>";
								$this->log->add_log_on_page("local_skip_fetch : $local_skip_fetch",true);
								if ($local_skip_fetch === true)	
								{
									$this->log->add_log_on_page("ATTENZIONE SKIPPATO! - local_skip_fetch: $local_skip_fetch - this->current_row_no : $this->current_row_no",true);
								}
								else
									$this->next_record();
							}  
							$this->log->add_log_on_page("function: '$a_function_name'  QUERY OK",true);
						}	
						
					} 
					else
					{
						if ($this->mysqli->error != '')
							$this->last_error = $this->mysqli->error;
						$this->function_result = $this->last_error; 	
						$this->log->add_log_error("Errore: '".$this->last_error."' su query: '".$local_sql."'",NULL,true);
					}
				}
				else
				{
					$this->log->add_log_error("Database not connected",NULL,true);					
					$this->last_error = "Database not connected";
				}
            }    
            catch(Exception $e)
            {
				$this->last_error = $e->getMessage();
                //$this->log->add_log_error("errore imprevisto eseguendo la funzione execute_function_from_query_result: ".$e->getMessage(),NULL,true);//$this->mysqli->error());
				$this->log->add_log_error($e,NULL,true);//$this->mysqli->error());
            }
            return $this->function_result;
           // if (!$this->connection->connected)
             //   $this->connection->close_connection();
		}		
        public function free_sql_result ()
		{
			if ((! isset($this->sql_result_query)) || ($this->sql_result_query == null) || (!is_object($this->sql_result_query)))
			  return;
			$this->sql_result_query->free();
			while ($this->mysqli->next_result()) 
			{
				if ($this->sql_result_query = $this->mysqli->store_result()) 
					$this->sql_result_query->free();
			} 
		}
		public function execute_function_from_even_with_empty_query_result ($a_function_obj,$a_function_name,$a_function_argouments = null)
		{   
            
           
          //  if (!$this->connection->connected)
                //$this->open_connection();
            //    
            $this->function_result = '';
            try
            {
                $local_is_query_without_result = $this->is_query_without_result();
				$local_sql = $this->retrive_sql_query_with_value_parameter($this->sql_str);
				$this->log->add_log_on_page("execute_function_from_even_with_empty_query_result - local_sql: $local_sql",true);
				//echo "local_sql: $local_sql";
				//echo "this->connection->connected:". $this->connection->connected;
				if ($this->connection->connected ) 
				{
					if ($this->is_sql_result_query_failed())
                    {
                        
						//$this->mysqli->multi_query($local_sql);
						$this->sql_result_query = $this->mysqli->multi_query($local_sql);
						if (!$this->is_query_without_result())
							$this->sql_result_query = $this->mysqli->store_result();
						
						if (!$this->is_sql_result_query_failed())
						{
							$this->current_row_no = -1;
							$this->bof_booked = false;
							$this->eof_booked = false;
							$this->next_record();
						}	
                    }
                    else
                    {
                        $this->first();
                    }
					
                    if (!$this->is_sql_result_query_failed())
					{   
						//echo "a_function_name: ".$a_function_name . "<br>";
						
						if (($this->has_sql_result_query_got_result_set()) || ($local_is_query_without_result))
						{
						
							$local_skip_fetch = false;
							if ($local_is_query_without_result)
								$local_rows_count = 0;
							else
								$local_rows_count = mysqli_num_rows($this->sql_result_query);
							$this->log->add_log_on_page("local_rows_count : $local_rows_count",true);
							
							//echo "local_rows_count: ". $local_rows_count ."<br>";
							//echo "this->current_row_no : $this->current_row_no <br>";
							//echo "local_skip_fetch: ".$local_skip_fetch ."<br>";
							if ($local_rows_count == 0)
							{
								//echo "local_rows_count: ". $local_rows_count ."<br>";
								$this->log->add_log_on_page("this->current_row_no : $this->current_row_no",true);
								$local_skip_fetch =  ($a_function_obj->$a_function_name($this,$this->current_row_no,$local_rows_count,
									$a_function_argouments) );
							}
							else
							{
								while (!$this->eof()) 
								{ 
									$this->log->add_log_on_page("this->current_row_no : $this->current_row_no",true);
									$local_skip_fetch =  ($a_function_obj->$a_function_name($this,$this->current_row_no,$local_rows_count,
										$a_function_argouments) );
									//echo "this->current_row_no : $this->current_row_no <br>";	
									//$this->log->add_log_on_page("local_skip_fetch : $local_skip_fetch",true);
									if ($local_skip_fetch === true)	
									{
										$this->log->add_log_on_page("ATTENZIONE SKIPPATO! - local_skip_fetch: $local_skip_fetch - this->current_row_no : $this->current_row_no",true);
									}
									else
										$this->next_record();
									/*if (!$local_skip_fetch)	
										$this->next_record();*/
								}  
							}
							$this->log->add_log_on_page("function: '$a_function_name'  QUERY OK",true);
						}
						else
						{
						
						}
					} 
					else
					{
						$this->last_error = $this->mysqli->error;
						$this->log->add_log_error("Errore eseguendo la query '".$local_sql."' a causa di: ".$this->last_error,NULL,true);
					}
				}
				else
				{
					$this->log->add_log_error("Database not connected",NULL,true);					
					$this->last_error = "Database not connected";
				}
            }    
            catch(Exception $e)
            {
				$this->last_error = $e->getMessage();
                //$this->log->add_log_error("errore imprevisto eseguendo la funzione execute_function_from_query_result: ".$e->getMessage(),NULL,true);//$this->mysqli->error());
				$this->log->add_log_error($e,NULL,true);//$this->mysqli->error());
            }
            return $this->function_result;
           // if (!$this->connection->connected)
             //   $this->connection->close_connection();
		}
		
        public function execute_query($a_no_parameters = false,$a_no_log = false)
		{   

            if ($this->connection->connected)
            {
                //echo "execute_query OK";
                if ($a_no_parameters === true)
                    $local_sql = $this->sql_str;
                else    
                    $local_sql = $this->retrive_sql_query_with_value_parameter($this->sql_str);
                //echo "local_sql: $local_sql - ";
			    $this->log->add_log_on_page("local_sql:$local_sql",true);
                //
                $this->current_row_no = -1;
                $this->bof_booked = false;
				$this->eof_booked = false;
                //
				$this->sql_result_query = $this->mysqli->multi_query($local_sql);
				if (!$this->is_query_without_result())
					$this->sql_result_query = $this->mysqli->store_result();
				
                if ($this->mysqli->error != '')
                {
                    //echo "execute_query ERRORE<br>";
					$this->last_error = $this->mysqli->error;
					if (!$a_no_log)
                        $this->log->add_log_error("Errore eseguendo la query '".$local_sql."' a causa di: ".$this->last_error,NULL,true);
						
					return false;
                }
				else
				{
					//echo "execute_query OK<br>";
					$this->log->add_log_on_page("QUERY OK",true);
                    
					return true;
                }
            }
			else
			{
				
				$this->log->add_log_error("Database not connected",NULL,true);					
				$this->last_error = "Database not connected";
			
				return false;
			}
		}  
        // F
        public function first()
		{   
            $local_result = false;
			try
            {
				if (($this->connection->connected ) && (!$this->is_sql_result_query_failed()))
				{                    
					if ($this->has_sql_result_query_got_result_set())
					{
						if (($this->current_row_no > -1) && (!mysqli_data_seek($this->sql_result_query, 0))) 
						{
							$this->last_error = $this->mysqli->error;
							$this->log->add_log_error(__METHOD__ . ": Cannot seek to first row: ". $this->last_error,NULL,true);
						}
						else
						{
							$this->current_row_no = -1;
							$this->bof_booked = false;
							$this->eof_booked = false;
							$local_result = $this->next_record();
							//echo "first OK<br>";
						}	
					}	
                }
				else
				{
					$this->log->add_log_error("Database not connected",NULL,true);					
					$this->last_error = "Database not connected";
				}
            }    
            catch(Exception $e)
            {
				$this->last_error = $e->getMessage();
                $this->log->add_log_error(__METHOD__ . ": errore imprevisto eseguendo la funzione first: ".$e->getMessage(),NULL,true);//$this->mysqli->error());
            }
            return $local_result;
		} 		
        // G
        public function get_field_value ($a_query_field)
        {
            $local_return = $this->sql_values[$a_query_field];
			return $local_return;

        }
        public function get_parameter_value($a_query_field,$a_position_of_value)
        {
			//echo "this->sql_values[0]:".$this->sql_values[0]." <br>";
            //echo "this->sql_values[a_query_field]:".$this->sql_values[$a_query_field]." <br>";
			$local_return = self::get_parameter_value_from_value($this->sql_values[$a_query_field],
				$a_position_of_value);
			return $local_return;
		}
		public static function get_parameter_value_from_value($a_value,$a_position_of_value)
        {
			global $g_strings_utility_namespace;
            //
            
			//echo "a_value: '$a_value'<br>";
            $local_return =$a_value;
            if ($a_position_of_value >= 0)
            {
                $local_position_array = explode(self::separator_level_character,
                    $a_position_of_value);
                $local_level = count($local_position_array)-1;
                if ($local_level >= self::db_position_of_value_level_0)
                {
                    $local_return = $g_strings_utility_namespace->extract_from_position(
                        $local_return,
                        self::separator_level_0,
                        $local_position_array[0]);
                }
                if ($local_level >= self::db_position_of_value_level_1)
                {
                    $local_return = $g_strings_utility_namespace->extract_from_position(
                        $local_return,
                        self::separator_level_1,
                        $local_position_array[1]);
                }
            }    
			return $local_return;

        }
        // G
		public static function get_field_type($a_type_flag)
        {
			$local_mysql_data_type_hash = array(
				1=>'tinyint',
				2=>'smallint',
				3=>'int',
				4=>'float',
				5=>'double',
				7=>'timestamp',
				8=>'bigint',
				9=>'mediumint',
				10=>'date',
				11=>'time',
				12=>'datetime',
				13=>'year',
				16=>'bit',
				252=>'blob',
				253=>'varchar',
				254=>'char',
				246=>'decimal'
			);			
			return $local_mysql_data_type_hash[$a_type_flag];
        }		
        public function get_last_error()
        {
           return addslashes($this->last_error);
        }
		public function get_parameter_index_by_name ($a_name)
		{   
			$local_return = -1;
			//echo "a_name:$a_name <br>";
			if (count($this->parameters_names) == 0)
			{
				$local_sql = $this->sql_str;
                $local_start = $this->get_char_parameter_position($local_sql); 
                if ($local_start)
                    $local_stop = $this->get_char_parameter_position($local_sql,$local_start+1); 
                else	
                    $local_stop = false;
                //
                //echo "local_start: $local_start - local_stop: $local_stop - ";
                $local_index = 0;
                while (($local_start) && ($local_stop))
                {
                    $local_param = substr($local_sql,$local_start+1,($local_stop - $local_start)-1);  
                    //echo "local_index: $local_index - local_param: $local_param### <br>";
                    $this->parameters_names[$local_index] = $local_param;
                    $local_value = $this->value_parameter[$local_index];
                    //
                    $local_start = $this->get_char_parameter_position($local_sql,$local_stop + 1); 
                    if ($local_start)
                        $local_stop = $this->get_char_parameter_position($local_sql,$local_start+1); 
                    else	
                        $local_stop = false;
                    //    
                    $local_index ++;    
                }               
            }
			if (isset($this->parameters_names))
			{
				foreach ($this->parameters_names as $local_parameter_index => $local_parameter)
				{
					//echo "local_parameter_index: $local_parameter_index - local_parameter: $local_parameter### <br>";
					if (strtoupper($local_parameter) == strtoupper($a_name))
					{
						$local_return = $local_parameter_index;
						break;
					}    
				}
			}
            
			return $local_return;
		}
		// I
        public function import_data_from_values_array($a_values_array)
        {
            unset($this->sql_values);
            $this->sql_values = $a_values_array;
        }
        public function is_first_record()
		{   
            $local_result = false;
			try
            {
				if ($this->has_sql_result_query_got_result_set())
				{
					if (($this->connection->connected ) && (!$this->is_sql_result_query_failed()))
					{                    
						$local_result = (($this->current_row_no + 1) == 1);
					}
					else
					{
						$this->log->add_log_error("Database not connected",NULL,true);					
						$this->last_error = "Database not connected";
					}
				}
            }    
            catch(Exception $e)
            {
				$this->last_error = $e->getMessage();
                $this->log->add_log_error(__METHOD__ . ": errore imprevisto eseguendo la funzione is_first_record: ".$e->getMessage(),NULL,true);//$this->mysqli->error());
            }
            return $local_result;
		}        
        public function is_last_record()
		{   
            $local_result = false;
			try
            {
			
				if (($this->connection->connected ) && (!$this->is_sql_result_query_failed()))
				{                    
					if ($this->has_sql_result_query_got_result_set())
					{
						$local_rows_count = mysqli_num_rows($this->sql_result_query);
						//echo "<br> local_rows_count: $local_rows_count - ";
						//echo "this->current_row_no + 1: ". ($this->current_row_no + 1) . " <br>";
						$local_result = (($this->current_row_no + 1) == $local_rows_count);
					}	
				}
				else
				{
					$this->log->add_log_error("Database not connected",NULL,true);					
					$this->last_error = "Database not connected";
				}
					
            }    
            catch(Exception $e)
            {
				$this->last_error = $e->getMessage();
                $this->log->add_log_error(__METHOD__ . ": errore imprevisto eseguendo la funzione is_last_record: ".$e->getMessage(),NULL,true);//$this->mysqli->error());
            }
            return $local_result;
		}
		
		public function is_not_correct_value($a_field,$a_value, $a_index)
		{   
            $local_result = flase;
			$local_value = trim($a_value);
            if ($this->connection->connected )
            { 
                
				$local_numeric_error = (($a_field->numeric == 1) && 
					((!is_numeric($local_value)) && (($a_field->not_null == 1) || (trim($local_value) != 'NULL')))); 
                if ($local_numeric_error)
                {
					echo "local_numeric_error - this->table_information: $this->table_information<br>"; 
                    if ((!$this->table_information) || ($this->mysqli->error != ''))
					{
                       
						if (!$this->mysqli->multi_query("SHOW COLUMNS FROM ". $a_field->table))
						{
							
							$this->log->add_log_error($this->mysqli->error,NULL,true);	
							echo "this->mysqli->error: $this->mysqli->error";
						}
						else	
						{
							
							$this->table_information = $this->mysqli->store_result();
						}
					}
                //    if ($this->fields_count == -1)
                  //      $this->fields_count = mysql_num_fields($this->sql_result_query);
				    
					if ((!$this->table_information) || ($this->mysqli->error != ''))
					{
						
						$this->log->add_log_error($this->mysqli->error,NULL,true);	
						echo "this->mysqli->error: $this->mysqli->error";
					}
					else
					{
						//echo "6----";
						//print_r($this->table_information);
						$local_numeric_error = (strpos($this->db_result($this->table_information, $a_index , 'Extra'), 'auto_increment') === FALSE);           
						$local_result = ($local_numeric_error);
					}
				}
				else
					$local_result = (($local_value == '') && ($a_field->not_null == 1));
                    

               //echo "local_value: $local_value - is_numeric(local_value):".is_numeric($local_value)." - a_field->numeric: $a_field->numeric - a_field->not_null: $a_field->not_null \n";
            }
            else
            {
                $this->log->add_log_error("Database not connected",NULL,true);					
                $this->last_error = "Database not connected";
            }	
            return $local_result;
		}
		public function is_query_without_result()
		{
			$local_sql = strtoupper(trim($this->sql_str));
			return ((strpos($local_sql, 'DELETE') === 0) || (strpos($local_sql, 'INSERT') === 0) || (strpos($local_sql, 'UPDATE') === 0));
		}
        // L
        public function locate($a_match_field,$a_value_to_match)
        {
            //echo "a_match_field: $a_match_field - a_value_to_match$a_value_to_match<br>";
            $local_result = -1;
			try
            {
				if (($this->connection->connected ) && (!$this->is_sql_result_query_failed()))
				{  
                    if ($this->has_sql_result_query_got_result_set())
					{
					   // echo "a_match_field: $a_match_field - a_value_to_match$a_value_to_match<br>";
						$this->first();
						//echo $this->sql_values[$a_match_field]." - $a_value_to_match<br>";
						while ((!$this->eof()) && ($this->sql_values[$a_match_field] != $a_value_to_match)) 
						{ 
							$this->next_record();
							//echo $this->sql_values[$a_match_field]." - $a_value_to_match<br>";
						}
						$local_result = $this->current_row_no;
					}	
                }
				else
				{
					$this->log->add_log_error("Database not connected",NULL,true);					
					$this->last_error = "Database not connected";
				}
            }    
            catch(Exception $e)
            {
				$this->last_error = $e->getMessage();
                $this->log->add_log_error(__METHOD__ . ": errore imprevisto eseguendo la funzione locate: ".$e->getMessage(),NULL,true);//$this->mysqli->error());
            }    
            return $local_result;
        }
        // N
        public function next_record()
		{   
            //echo "next_record<br>";
			$local_result = false;
			try
            {
				if (($this->connection->connected ) && (!$this->is_sql_result_query_failed()))
				{   
					if ($this->has_sql_result_query_got_result_set())
					{
						$local_rows_count = mysqli_num_rows($this->sql_result_query);
						// 
						if (! $this->is_last_record())
						{ 
						   // echo "NOT last_record OK";
							$this->sql_values = mysqli_fetch_array($this->sql_result_query,$this->result_type/*,MYSQL_ASSOC*/);
							$this->current_row_no++;
							$this->bof_booked = false;
							$this->eof_booked = false;
							$local_result = true;
						}
						else
						{
							//echo "last_record OK";
							$this->bof_booked = false;
							$this->eof_booked = true;
							
						}
					}	
                }
				else
				{
					
					$this->log->add_log_error("Database not connected",NULL,true);					
					$this->last_error = "Database not connected";
				}
            }    
            catch(Exception $e)
            {
				//echo "ERRORE!!!!<br>";
				$this->last_error = $e->getMessage();	
                $this->log->add_log_error(__METHOD__ . ": errore imprevisto eseguendo la funzione next_record: ".$e->getMessage(),NULL,true);//$this->mysqli->error());
            }
			return $local_result;
		}       
        
		// O
		public function open_connection ()
		{   
            $this->mysqli = $this->connection->open_connection();
            return $this->connection;
        }
        // R
		/*public function retrive_data_from_query()
		{   
           // echo "connected: ".$this->connected;
            //echo "sql_result_query: ".$this->sql_result_query;
            if (($this->connection->connected) && ($this->sql_result_query))
            {
                //
                unset($this->sql_values);
				$this->sql_values = mysql_fetch_array($this->sql_result_query/*,MYSQL_ASSOC*//*);
				$this->current_row_no++;
                return true;
            }   
            return false;            
		}*/
		public function retrive_sql_query_with_value_parameter ($a_sql)
		{   
			global $g_ambient_namespace;
			global $g_url_parameters_namespace;
			global $g_strings_utility_namespace;
			global $g_cuteengine;
			$local_site_folder = '';
			$local_sql = $a_sql;
			$local_start = $this->get_char_parameter_position($local_sql); 
			if ($local_start)
				$local_stop = $this->get_char_parameter_position($local_sql,$local_start+1); 
			else	
				$local_stop = false;
			//
            //echo "local_start: $local_start - local_stop: $local_stop - ";
            $local_index = 0;
			while (($local_start) && ($local_stop))
			{
				$local_param = substr($local_sql,$local_start,($local_stop - $local_start)+1);  
				
                $local_count = 1;
                $local_value = $this->value_parameter[$local_index];
				//echo "local_param: $local_param - local_value: $local_value<BR>";
                $local_sql = str_replace($local_param,$local_value,$local_sql, $local_count);
				//echo "dopo local_sql: $local_sql<BR>
				//";
                //
                $local_start = $this->get_char_parameter_position($local_sql,$local_start + strlen($local_value)); 
				if ($local_start)
					$local_stop = $this->get_char_parameter_position($local_sql,$local_start+1); 
				else	
					$local_stop = false;
                //    
                $local_index ++;    
			}
			if ($this->parent_component_class != 't_component_configurator')
			{
				
				/*if (SITE_FOLDER != '')
					$local_site_folder = SITE_FOLDER.'\\\\';
				$local_from = array("__CE_SITE_FOLDER__", 
					"__CE_CURRENT_URL__", 
					"__CE_CURRENT_URL_DEPTH_1__", 
					"__CE_CURRENT_URL_DEPTH_2__", 
					"__CE_PARENT_PAGE_URL__",
					"__CE_SEPARATOR_LEVEL_0__",
					"__CE_SEMICOLON__",
					"__CE_USER_ID__");
				$local_to = array($local_site_folder, 
					$g_ambient_namespace->get_absolute_web_file_path(false).$g_url_parameters_namespace->compute_disguised_parameters_string(),
					"__CE_CURRENT_URL__",
					"__CE_CURRENT_URL_DEPTH_1__", 
					$g_cuteengine->parent_page_url,
					self::separator_level_0,
					":",
					$g_cuteengine->user->id);
				
				//print_r($local_from);
				//echo "g_cuteengine->user->id: ".$g_cuteengine->user->id."<br>";	
				//print_r($g_cuteengine->user);
				//echo "local_sql: $local_sql<br>";*/
				$local_sql = $g_strings_utility_namespace->get_disguised_value($local_sql);
			//	$local_sql = str_replace($local_from,$local_to,$local_sql);
				//echo "local_sql: $local_sql<br>";
				// echo "local_sql: $local_sql - ";
				$local_sql = $g_url_parameters_namespace->add_argument_value_to_str($this->parameter_index_for_argument_value,$local_sql, 4);
			}	
			return $local_sql;
		}
        // P
        public static function prepare_table_name($a_table_name)
        {
            $local_return = str_replace('`','',$a_table_name);
            $local_array = explode('.', $local_return);
            return $local_array[count($local_array)-1];
        }
        
        public function previous_record()
		{   
            $local_result = false;
			try
            {
				if (($this->connection->connected ) && (!$this->is_sql_result_query_failed()))
				{                    
                    if ($this->has_sql_result_query_got_result_set())
					{
						$local_rows_count = mysqli_num_rows($this->sql_result_query);
						// 
						if (! $this->is_first_record())
						{ 
							$this->sql_values = mysqli_fetch_array($this->sql_result_query,$this->result_type/*,MYSQL_ASSOC*/);
							$this->current_row_no--;
							$this->bof_booked = false;
							$this->eof_booked = false;
							$local_result = true;
						}
						else
						{
							$this->bof_booked = true;
							$this->eof_booked = false;
						}
					}	
                }
				else
				{
					$this->log->add_log_error("Database not connected",NULL,true);					
					$this->last_error = "Database not connected";
				}
            }    
            catch(Exception $e)
            {	
				$this->last_error = $e->getMessage();
                $this->log->add_log_error(__METHOD__ . ": errore imprevisto eseguendo la funzione previous_record: ".$e->getMessage(),NULL,true);//$this->mysqli->error());
            }
			return $local_result;
		}  
		//P
		public function put_query_field_into_url_parameter($a_query_parameter,$a_current_field,
			$a_current_field_no,$a_fields_count,$a_current_row_no,$a_rows_count,$a_url_parameter)
        {	
                //print_r($a_url_parameter->argument);
                $local_destination_index = 3; //$a_url_parameter->get_arguments_count();
                //echo "local_destination_index: $local_destination_index - a_query_parameter->sql_values[a_current_field_no]: ".$a_query_parameter->sql_values[$a_current_field_no]."\n";
                $a_url_parameter->append_to_argument_value($local_destination_index,$a_query_parameter->sql_values[$a_current_field_no]);

		}
		public function put_query_result_into_url_parameter($a_query_parameter,$a_current_row_no,$a_rows_count,$a_url_parameter)
        {	
				$a_query_parameter->execute_function_from_fields_result($this,'put_query_field_into_url_parameter',$a_url_parameter);

		}        
        // Q
        public function query_num_rows ()
		{   
            $local_result = 0;
           // echo "(".$this->connected.") && (".$this->sql_result_query.")";
            if (($this->connection->connected) && (!$this->is_sql_result_query_failed()))
            {
				if ($this->has_sql_result_query_got_result_set())
					$local_result = mysqli_num_rows($this->sql_result_query);
                //echo "local_result: $local_result - ";
            }    
            return $local_result;
		}  
		// R
		public function reset_parameters ()
		{   
            unset($this->value_parameter); 
			unset($this->parameters_names);
           // echo "count:". count($this->value_parameter);
		} 
		// S
		public function set_field_value_to_text ($a_source_text)
		{   
            $local_result = $a_source_text;
            if ($this->connection->connected ) 
			{
				if (!$this->is_sql_result_query_failed())
                {
                    if ($this->has_sql_result_query_got_result_set())
					{
						$i = 0;
						if ($this->fields_count == -1)
						{
							$local_skip_field = false;
							$this->fields_count = mysqli_num_fields($this->sql_result_query);
						}
						else
						{
							$local_skip_field = true;
						}
						$local_field_name = array();
						$local_field_value = array();
						while ($i < $this->fields_count) 
						{
							//echo "Information for column $i:<br />\n";
							if (!$local_skip_field)
							{
								$local_field = mysqli_fetch_field_direct($this->sql_result_query, $i);
								$this->fields_list[] = $local_field;
							}
							$local_field_name[] = ":".$this->fields_list[$i]->name.":";
							$local_field_value[] = $this->sql_values[$i];
							$i++;
						} 		
						$local_result = str_replace($local_field_name, $local_field_value, $local_result);
					}	
                }
                else
                {
                    $this->log->add_log_error('$this->sql_result_query not valid',NULL,true);					
                    $this->last_error = '$this->sql_result_query not valid';
                }
            }
			else
			{
				$this->log->add_log_error("Database not connected",NULL,true);					
				$this->last_error = "Database not connected";
			}	
            return $local_result; 
		}  
		public function set_value_last_parameter ($a_value)
		{   
            $local_index = (count($this->value_parameter)-1);
			$this->value_parameter[$local_index] = $this->mysqli->real_escape_string($a_value); 
		}        
		public function set_value_parameter ($a_value,$a_index)
		{   
			$this->value_parameter[$a_index] = $this->mysqli->real_escape_string($a_value); 
		}
		public function start_transaction ()
		{ 
			/*if (($this->connection->connected) && (!$this->in_transaction))
			{
				mysql_query("SET autocommit=0;");
				try
				{
					mysql_query("START TRANSACTION;");
                    echo "START TRANSACTION<br>";
					$this->in_transaction = true;
				}	
				catch(Exception $e)
				{
					mysql_query("SET autocommit=1;");
					$this->last_error = $e->getMessage();	
					$this->log->add_log_error(__METHOD__ . ": unexpected error starting transaction: ".$e->getMessage(),NULL,true);//$this->mysqli->error());
				}					
			}	*/
		}
		public function stop_transaction ($a_value)
		{ 
			/*if (($this->connection->connected) && ($this->in_transaction))
			{
				try
				{
					if ($a_value)
                    {
                        mysql_query("COMMIT;");
                        echo "COMMIT<br>";
                    }    
					else
                    {
						mysql_query("ROLLBACK;");
                        echo "ROLLBACK<br>";
                    }    
				}	
				catch(Exception $e)
				{
					$this->last_error = $e->getMessage();	
					$this->log->add_log_error(__METHOD__ . ": unexpected error closing transaction: ".$e->getMessage(),NULL,true);//$this->mysqli->error());
				}	
				$this->in_transaction = false;
				mysql_query("SET AUTOCOMMIT=1");

			}	*/
		}
        
		
	}
?>